package cn.zsssgjcx.hjsg.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

import cn.zsssgjcx.hjsg.R;
import cn.zsssgjcx.hjsg.model.CityEntity;

public class SearchCityListAdapter extends BaseAdapter {
    private List<CityEntity> cityEntities;
    private LayoutInflater inflater;

    public SearchCityListAdapter(Context mContext, List<CityEntity> cityEntities) {
        this.cityEntities = cityEntities;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public int getCount() {
        return cityEntities == null ? 0 : cityEntities.size();
    }

    @Override
    public Object getItem(int position) {
        return cityEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (null == convertView) {
            convertView = inflater.inflate(R.layout.city_list_item_layout, null);
            holder = initView(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        CityEntity cityEntity = cityEntities.get(position);
        holder.cityKeyTv.setVisibility(View.GONE);
        holder.cityNameTv.setText(cityEntity.getName());

        return convertView;
    }

    private ViewHolder initView(View v){
        ViewHolder holder = new ViewHolder();
        holder.cityNameTv = v.findViewById(R.id.city_name_tv);
        holder.cityKeyTv = v.findViewById(R.id.city_key_tv);
        return holder;
    }

    private class ViewHolder {

        TextView cityNameTv;
        TextView cityKeyTv;
    }
}
